package zhu.areas.user.action;

import java.io.IOException;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import zhu.areas.user.data.User;
import zhu.areas.user.service.IUserService;
import zhu.common.service.MailService;
import zhu.common.service.IDicService;
import com.opensymphony.xwork2.ActionSupport;

/***
 * 
 * 需要获取的userID,username保存在了session中，key分别为userID，username
 * @author h1x2m3
 *
 */
public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// private Integer userID;
	//登录界面传进来的变量
	private String username;
	private String email;
	private String repassword;
	
	//账户安全修改密码传递的变量
	private String newpassword;
	private String renewpassword;

	private User user = new User();

	private IUserService userService;
	private MailService mailService;
	
	
	

	public String mainTain() {
		return SUCCESS;
	}

	public String changeToRegister() {
		return SUCCESS;
	}

	public String changeToLogin() {
		return SUCCESS;
	}

	//用户注册
	public String register() {
		this.clearErrorsAndMessages();
		if (!userService.nameUnique(user.getUsername())) {
			addFieldError("user.username", "用户名已存在");
			return ERROR;
		}

		if (!userService.emailUnique(user.getEmail())) {
			addFieldError("user.email", "邮箱已被使用");
			return ERROR;
		}
		
		user.setRegTime(new Date());
		if (userService.addUser(user)) {
			// 用户注册成功后，异步发送邮件
			mailService.sendAsyncMail(user.getEmail());
			
			int userID = userService.findByUsername(user.getUsername()).getUserID();
			ServletActionContext.getRequest().getSession().setAttribute("userID", userID);
			ServletActionContext.getRequest().getSession().setAttribute("userName", user.getUsername());
			//System.out.println(user.getEmail());
			return SUCCESS;
		} else {
			System.out.println("添加用户失败");
			addActionError("添加用户失败");
			return ERROR;
		}
	}

	// 检验用户名唯一性
	public void nameUniqueCheck() throws IOException {
		int result = 0;// 0表示正确
		System.out.println(username);
		if (username == null || username == "") {
			result = 1;// 1表示输入为空
		} else {
			if (!userService.nameUnique(username)) {
				result = 2;// 2表示已被占用
			}
		}
		// 向客户端传递数据
		ServletActionContext.getResponse().getWriter().print(result);
	}

	// 检验邮箱唯一性
	public void emailUniqueCheck() throws IOException {
		System.out.println(email);
		int result = 0;// 0表示正确
		System.out.println(email);
		if (email == null || email.equals("") || email == "") {
			result = 1;// 1表示输入为空
		} else {
			if (!userService.emailUnique(email)) {
				result = 2;// 2表示已被占用
			}
		}
		// 向客户端传递数据
		ServletActionContext.getResponse().getWriter().print(result);
	}

	//登录系统
	public String login() {
		User user2 = userService.findByUsername(user.getUsername());
		System.out.println(user2);
		if (user2 == null) {
			addFieldError("user.username", "该用户不存在，请重新输入");
			return ERROR;
		} else {
			if (!(user2.getPassword().equals(user.getPassword()))) {
				System.out.println("密码不正确，请重新输入");
				addFieldError("user.password", "用户名或密码不正确，请重新输入");
				return ERROR;
			} else {
				ServletActionContext.getRequest().getSession().setAttribute("userID", user2.getUserID());
				ServletActionContext.getRequest().getSession().setAttribute("userName", user.getUsername());
			}
		}
		return SUCCESS;
	}
	
	//账户安全，修改密码
	public String updatePassword(){
		User dbUser=new User();
		dbUser=userService.findByUsername((String) ServletActionContext.getRequest().getSession().getAttribute("userName"));
		if(user.getPassword().equals(dbUser.getPassword())){
			dbUser.setPassword(newpassword);
			if(userService.updateUser(dbUser)){
				System.out.print("密码修改成功 ");
				return SUCCESS;
			}
			else{
				System.out.println("密码修改失败");
				return ERROR;
			}
		}
		else{
			System.out.println("密码输入错误");
			return ERROR;
		}
	}
	//退出系统
	public String logout() {
		ServletActionContext.getRequest().getSession()
				.removeAttribute("userId");
		ServletActionContext.getRequest().getSession()
				.removeAttribute("userName");
		return SUCCESS;
	}
	
	//进入个人主页
	public String perIndex(){
		return SUCCESS;
	}
	
//	public void  testHibernate(){
//		System.out.println(userService.getUsers().size());
//		//System.out.println(userService.findByUsername("123456").getTenderDisps2().size());	
//	}

	public String update() {
		return SUCCESS;
	}

	public String delete() {
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public MailService getMailService() {
		return mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getRenewpassword() {
		return renewpassword;
	}

	public void setRenewpassword(String renewpassword) {
		this.renewpassword = renewpassword;
	}
	
	

}


